<template>
	<view>
		<view class="header-title">点击对象进行编辑,可拖动</view>
		<view class="pic" :style="{height:aheight+'px'}" v-if="shows">
			<image class="test" :src="imgurl+'test.png'"></image>
			<view class="title" @touchstart="drag_start" @touchmove="drag_hmove" :style="{fontSize:titlesize+'rpx',Top:moveY+'px',Left:moveX+'px'}"
			 @tap="showPopup(2)">{{title}}</view>
			<view class="qr-img" :style="{height:size+30+'px',top:qrmoveY+'px',left:qrmoveX+'px'}" @touchstart="qrstart"
			 @touchmove="qrmove">
				<view class="qr-img-i flex" :style="{height:size+'px'}" @tap.stop="showPopup(3)">
					<tki-qrcode cid="qrcode1" ref="qr" :val="val" :size="size" :unit="unit" :background="background" :foreground="foreground"
					 :pdground="pdground" :onval="onval" :loadMake="loadMake" :usingComponents="true" @result="qrR" />
				</view>
				<view>扫码进入问卷界面</view>
			</view>
		</view> 
		<view v-else class="canvas-box" @longpress="saveImage">
			<canvas :style="{height:aheight+'px',width:awith+'px'}" canvas-id="firstCanvas"></canvas></view>
		<view class="footer row" @tap="shareFrends">
			<image :src="imgurl+'createpic.png'"></image>
			<text>生成图片</text>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="popup" v-if="show===3">
				<view class="uni-padding-wrap uni-common-mt">
					<view class="uni-title">设置二维码大小</view>
				</view>
				<view class="body-view">
					<slider :value="size" @change="sliderchange" min="100" max="200" show-value />
				</view>
				<view class="close" @tap="close">关闭</view>
			</view>
			<view class="popup" v-else-if="show===2">
				<view class="uni-padding-wrap uni-common-mt">
					<view class="uni-title">设置标题大小</view>
				</view>
				<view class="body-view">
					<slider :value="titlesize" @change="titlechange" min="40" max="100" show-value />
				</view>
				<view class="close" @tap="close">关闭</view>
			</view>
			<view class="popup" v-else>
				<view class="uni-padding-wrap uni-common-mt">
					<view class="uni-title">图片大小</view>
				</view>
				<view class="close" @tap="close">关闭</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import tkiQrcode from '@/components/tki-qrcode/tki-qrcode.vue'
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import app from "../../App.vue"
	export default {
		components: {
			uniPopup,
			tkiQrcode
		},
		data() {
			return {
				imgurl: '',
				aheight: '',
				awith: "",
				start: [0, 0],
				moveY: 25,
				moveX: 25,
				qrmoveY: 200,
				qrmoveX: 100,
				shows: true,
				show: 1,
				title: "",
				val: '', //要生成的二维码值
				size: 100, // 二维码大小
				titlesize: 40,
				unit: 'rpx', // 单位
				background: '#b4e9e2', // 背景色
				foreground: '#309286', // 前景色
				pdground: '#32dbc6', // 角标色
				icon: '', // 二维码图标
				iconsize: 40, // 二维码图标大小
				onval: false, // val值变化时自动重新生成二维码
				loadMake: true, // 组件加载完成后自动生成二维码
				src: '', 				// 二维码生成后的图片地址或base64
				picurl: "",
				cover: app.globalData.imgurl + "test.png",
				saveimg: ""
			};
		},
		methods: {
			sliderchange(e) {
				this.size = e.detail.value
			},
			titlechange(e) {
				this.titlesize = e.detail.value
			},
			showPopup(e) {
				this.show = e
				this.$refs.popup.open()
			},
			//长按保存
			saveImage() {
				let that = this
				uni.saveImageToPhotosAlbum({
					filePath: that.saveimg,
					success() {
						uni.showToast({
							title: '保存成功',
							icon: "none"
						})
					},
					fail() {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					}
				})
			},
			close() {
				this.$refs.popup.close()
			},
			//移动标题
			drag_start(event) {
				this.start[0] = event.touches[0].clientX - event.target.offsetLeft;
				this.start[1] = event.touches[0].clientY - event.target.offsetTop;
			},
			//移动标题
			drag_hmove(event) {
				let tag = event.touches;
				if (tag[0].clientX < 0) {
					tag[0].clientX = 0
				}
				if (tag[0].clientY < 0) {
					tag[0].clientY = 0
				}
				if (tag[0].clientX > this.awith) {
					tag[0].clientX = this.awith
				}
				if (tag[0].clientY > this.aheight) {
					tag[0].clientY = this.aheight
				}
				this.moveX = tag[0].clientX - this.start[0];
				this.moveY = tag[0].clientY - this.start[1];
				if (this.moveX < 0) {
					this.moveX = 0
				}
				if (this.moveY < 0) {
					this.moveY = 0
				}
			},
			//移动二维码
			qrstart(event) {
				this.start[0] = event.touches[0].clientX - event.currentTarget.offsetLeft;
				this.start[1] = event.touches[0].clientY - event.currentTarget.offsetTop;
			},
			//移动二维码
			qrmove(event) {
				let tag = event.touches;
				if (tag[0].clientX < 0) {
					tag[0].clientX = 0
				}
				if (tag[0].clientY < 0) {
					tag[0].clientY = 0
				}
				if (tag[0].clientX > this.awith - this.size) {
					tag[0].clientX = this.awith - this.size
				}
				if (tag[0].clientY > this.aheight - this.size / 2 - 15) {
					tag[0].clientY = this.aheight - this.size / 2 - 15
				}
				this.qrmoveX = tag[0].clientX - this.start[0];
				this.qrmoveY = tag[0].clientY - this.start[1];
				if (this.qrmoveX < 0) {
					this.qrmoveX = 0
				}
				if (this.qrmoveY < 0) {
					this.qrmoveY = 0
				}
			},
			qrR(path) {
				this.picurl = path;
			},
			getDescBox() {
				uni.createSelectorQuery().in(this).select('.pic').boundingClientRect(result => {
					if (result) {
						console.log(result)
					} else {
						this.getDescBox()
					}
				}).exec();
			},
			shareFrends() {
				let that = this
				that.shows = false
				that.drawCanvas()
			},
			// 绘制canvas
			drawCanvas() {
				var that = this
				var windowW = that.awith
				var windowH = that.aheight
				var text = that.title
				// 使用 wx.createContext 获取绘图上下文 context
				var context = wx.createCanvasContext('firstCanvas')
				// 海报背景图
				context.drawImage(that.cover, 0, 0, windowW, windowH)
				context.setFontSize(that.titlesize / 2)
				context.setFillStyle("#fff")
				context.fillText(text, that.moveX, that.moveY)
				context.fillText(text, that.moveX, that.moveY)
				context.fillText(text, that.moveX, that.moveY)
				// 识别小程序二
				context.drawImage(that.picurl, that.qrmoveX, that.qrmoveY, that.size, that.size)
				context.setFillStyle("#fff")
				context.setFontSize(13)
				context.fillText('长按识别小程序', that.qrmoveX + (that.size - 100) / 2, that.qrmoveY + that.size + 20, (windowH + that.size +
					that.size + 40) / 2, 100)
				// context.draw()
				context.draw(false, () => {
					uni.canvasToTempFilePath({
						x: 0,
						y: 0,
						width: that.aheight,
						height: uni.upx2px(1330),
						destWidth: that.aheight,
						destHeight: uni.upx2px(1330),
						canvasId: 'firstCanvas',
						success: function(res) {
							that.saveimg = res.tempFilePath
						},
						fail(e) {
							uni.showToast({
								title: '生成海报失败',
								icon: 'none'
							});
						}
					});
				});
			},
		},
		mounted() {
			this.getDescBox();
		},
		onLoad(options) {
			let wenid = options.wenid
			this.title = options.title
			let url = options.url
			this.val = url + '?wenid=' + wenid
			console.log(this.val, 'VAL')
			var that = this;
			that.imgurl = app.globalData.imgurl
		},
		onShow() {
			let that = this
			this.$refs.qr._makeCode()
			uni.getSystemInfo({
				success(res) {
					that.aheight = res.windowHeight * 0.85
					that.awith = res.windowWidth
				}
			})
		}
	}
</script>

<style lang="scss">
	.header-title {
		width: 690rpx;
		margin: 0 auto;
		color: #666;
		font-size: 24rpx;
	}

	.canvas-box {

		background: #999999;

		position: fixed;

		top: 0;
		z-index: 100;
	}

	.pic {
		width: 690rpx;
		margin: 30rpx auto;
		height: 80%;
		position: relative;

		image {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
		}

		.title {
			color: #fff;
			font-weight: bold;
			font-size: 40rpx;
			// height: 200rpx;
			display: inline-block;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 10;
		}
	}

	.qr-img {
		// width: 690rpx;
		position: absolute;
		z-index: 1000;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		view {
			color: #fff;
			font-size: 30rpx;
			text-align: center;
		}
	}

	.qr-img-i {
		width: 100%;
		// height:200rpx;
		justify-content: center;
		box-sizing: border-box;
	}

	.popup {
		width: 100%;
		background: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		// height: 200rpx;
	}

	.footer {
		width: 690rpx;
		margin: 20rpx auto;
		justify-content: center;
		font-size: 30rpx;
		color: #333;

		image {
			width: 50rpx;
			height: 50rpx;
		}
	}

	.wrapper {
		height: 700rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 50upx;
	}

	.body-view {
		line-height: 60rpx;
	}

	.close {
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		border-top: 1rpx solid #DFDFDF;
	}

	.scroll {
		height: 150rpx;
		padding: 0 20rpx;

		.add {
			width: 150rpx;
			height: 120rpx;
			margin-top: 15rpx;
			border: 1rpx solid #DFDFDF;
			justify-content: center;

			image {
				width: 50rpx;
				height: 50rpx;
			}
		}
	}
</style>
